﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace YXShop.Web.admin.store
{
    public partial class store_contact_edit : System.Web.UI.Page
    {
        YXShop.BLL.Store.Store storeBll = new YXShop.BLL.Store.Store();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int id = ChangeHope.WebPage.PageRequest.GetQueryInt("id");
                InitWebControl();
                if (id > 0)
                {
                    GetStore(id);
                }
            }
        }

        private void GetStore(int id)
        {
            YXShop.Model.Store.Store s = new Model.Store.Store();
            YXShop.BLL.Admin.Administrators admin = new BLL.Admin.Administrators();
            s = storeBll.GetModelById(id);
            lblName.Text = s.Storename;
            lblAdminName.Text = admin.GetModel(s.Adminid.Value).Name;
            lblType.Text = YXShop.Common.ValuePair.GetStoreType(s.Storetype.Value);
            txtAddress.Text = s.Address;
            txtZipCode.Text = s.Zipcode;
            txtMobilePhone.Text = s.Mobilephone;
            txtPhone.Text = s.Phone;
            txtContact.Text = s.Contact;
            txtFax.Text = s.Fax;
        }

        #region 验证
        private void InitWebControl()
        {
            bool isEdit=true;
            if (ChangeHope.WebPage.PageRequest.GetQueryString("isedit") == "1")
                isEdit = true;
            else
                isEdit = false;
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();

            if (isEdit)
            {

                ChangeHope.WebPage.WebControl.Validate(this.txtZipCode, "邮政编码", "ispostalcode", "需符合邮政编码格式", "该项需符合邮政编码格式");
                ChangeHope.WebPage.WebControl.Validate(this.txtFax, "传真", "isfaxorphone", "需符合传真号码格式", "该项需符合传真号码格式");
                ChangeHope.WebPage.WebControl.Validate(this.txtMobilePhone, "手机号码", "ismobilephone", "需符合手机号码格式", "该项需符合手机号码格式");
                ChangeHope.WebPage.WebControl.Validate(this.txtPhone, "电话", "isfaxorphone", "需符合电话号码格式", "该项需符合电话号码格式");
            }
            txtAddress.ReadOnly = !isEdit;
            txtZipCode.ReadOnly = !isEdit;
            txtContact.ReadOnly = !isEdit;
            txtFax.ReadOnly = !isEdit;
            txtMobilePhone.ReadOnly = !isEdit;
            txtPhone.ReadOnly = !isEdit;
            btnSave.Visible = isEdit;
            Button1.Visible = isEdit;
        }
        #endregion

        #region 保存数据
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }


        private void Save()
        {
            int id = ChangeHope.WebPage.PageRequest.GetQueryInt("id");
            
            YXShop.BLL.Admin.Administrators adminBll = new YXShop.BLL.Admin.Administrators();
            YXShop.Model.Store.Store store = storeBll.GetModelById(id);

            store.Updatetime = DateTime.Now;
            store.Address = txtAddress.Text;
            store.Zipcode = txtZipCode.Text;
            store.Mobilephone = txtMobilePhone.Text;
            store.Phone = txtPhone.Text;
            store.Contact = txtContact.Text;
            store.Fax = txtFax.Text;

            int count=0;
            if (id > 0)
            {
                store.Sid = id;
                count = storeBll.Update(store);
            }
            if (count > 0)
            {
                if (id > 0)
                {
                    this.ltlMsg.Text = "操作成功，已修改该信息";
                    Response.Redirect(Request.Url.ToString());
                }
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionOk";
            }
            else
            {
                this.ltlMsg.Text = "操作失败";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
            }

        }
        #endregion



    }
}
